var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden:false,
    curNav: 1224292263651201024,
    curIndex: 0,
    cart: [],
    cartTotal: 0,
    count:0,
    pageNo:1,
    pageSize:5,
    navList: [],
    dishesList: [],
    dishes: [],
    hotOrRecomm:[],
    conversation:[],
    height:'',
    searchText:'',
    itemizeDit: '',
    hotOrRecommDit: '',
    server:app.data.server,
    aliyunOss: app.data.aliyunOss,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    this.loadingChange();
    
    var _this=this;
    var count=this.data.count;
    var pageNo=this.data.pageNo;
    var pageSize=this.data.pageSize;
    app.paiRequest("/app/topic/conversation/pageData", { hotOrRecomm: 1, pageNo: pageNo, pageSize: pageSize }).then(function (res) {
      var hotOrRecomm = res.data.hotOrRecomm;
      var conversation = res.data.conversation;
      var topicConversate = res.data.topicConversate;
      _this.setData({
        hotOrRecomm: hotOrRecomm,
        conversation: conversation,
        count: topicConversate.count,
        dishesList: topicConversate.list,
        curNav: hotOrRecomm[0].id,
        itemizeDit: '',
        hotOrRecommDit: hotOrRecomm[0].dictValue
      })
    })
   
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          height: res.windowHeight
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadingChange: function() {    
    setTimeout(() => {
      this.setData({
        hidden: true
      })
    }, 2000)
  },
  hotOrRecomm:function(event){
    var _this=this;
    let id = event.target.dataset.id, index = parseInt(event.target.dataset.index), hotOrRecomm = parseInt(event.target.dataset.dictvalue);
    this.setData({
      curNav: id,
      curIndex: index,
      pageNo: 1
    })
    var pageSize = this.data.pageSize;
    app.paiRequest("/app/topic/conversation/findPage", { hotOrRecomm: hotOrRecomm, pageNo: 1, pageSize: pageSize}).then(function (res) {
      var topicConversate = res.data;
      _this.setData({
        count: topicConversate.count,
        dishesList: topicConversate.list,
        itemizeDit: '',
        hotOrRecommDit: hotOrRecomm
      })
    })
  },
  selectNav:function(event){
    var _this = this;
    let id = event.target.dataset.id, index = parseInt(event.target.dataset.index), itemize = parseInt(event.target.dataset.dictvalue);
    self=this;
    this.setData({
      curNav:id,
      curIndex:index,
      itemizeDit: itemize,
      hotOrRecommDit: '',
      pageNo: 1
    })
    var pageSize = this.data.pageSize;
    app.paiRequest("/app/topic/conversation/findPage", { itemize: itemize, pageNo: 1, pageSize: pageSize }).then(function (res) {
      var topicConversate = res.data;
      _this.setData({
        count: topicConversate.count,
        dishesList: topicConversate.list
      })
    })
  },
  setStatus(dishId) {
    let dishes = this.data.dishesList;
    for (let dish of dishes) {
      dish.forEach((item) => {
        if (item.id == dishId) {
          item.status = !item.status || false
        }
      })
    }

    this.setData({
      dishesList: this.data.dishesList
    })
  },
  navigaTo:function(e){
    console.log(e);
    var src=e.currentTarget.dataset.src;
    let dataList = JSON.stringify(this.data.conversation)
    wx.navigateTo({
      url:src + '?dataList=' + dataList,
    })
  },
  selectDish: function (event) {
    console.log(event)
    let id = event.currentTarget.dataset.dish;
    let src = event.currentTarget.dataset.src;
    wx.navigateTo({
      url: src + '?id=' + id,
    })
  },
  onScrollLower:function(){
    var _this=this;
    var pageNo=this.data.pageNo+1;
    var pageSize=this.data.pageSize;
    var dishesList = this.data.dishesList;
    var count = this.data.count;
    var lent = dishesList.length;
    var itemizeDit=this.data.itemizeDit;
    var hotOrRecommDit = this.data.hotOrRecommDit;
    if (count == lent){
      wx.showToast({
        title: '到底了',
        icon:'success',
        duration:1000
      })
    }else{      
      app.paiRequest("/app/topic/conversation/findPage", { itemize: itemizeDit, hotOrRecomm: hotOrRecommDit, pageNo: pageNo, pageSize: pageSize}).then(function (res) {
      var topicConversate = res.data;
      var cont = dishesList.concat(topicConversate.list);      
      _this.setData({
        count: topicConversate.count,
        dishesList: cont,
        pageNo: pageNo
      })
    })    
    }
  },
  tapSearch:function(){
    var _this = this;
    var pageNo = this.data.pageNo;
    var pageSize = this.data.pageSize;
    var dishesList = this.data.dishesList;
    var searchText=this.data.searchText;
    var itemizeDit = this.data.itemizeDit;
    var hotOrRecommDit = this.data.hotOrRecommDit;
    console.log(searchText)
    app.paiRequest("/app/topic/conversation/findPage", { itemize: itemizeDit, hotOrRecomm: hotOrRecommDit,topicTitle: searchText, pageNo: pageNo, pageSize: pageSize }).then(function (res) {
      console.log(res)
      var topicConversate = res.data;
      var cont = topicConversate.list;
      _this.setData({
        count: topicConversate.count,
        dishesList: cont
      })
    })
  },
  bindSearchInput:function(e){
    var inputVal=e.detail.value;
    this.setData({
      searchText:inputVal
    })
  },
  imageerror:function(e){
    console.log(e);
    var index=e.currentTarget.dataset.index;
    var imagepath=e.currentTarget.dataset.imgpath;
    var dishesList = this.data.dishesList;
    var server=this.data.server;
    dishesList[index].topicImgPath = server + imagepath;
    this.setData({
      dishesList: dishesList
    })
  }
})